<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty {pp_inputzones} function plugin
 *
 * Type:     function<br>
 * Name:     pp_inputzones<br>
 * Input:<br>
 *           - name       (optional) - string default "select"
 *           - values     (required if no options supplied) - array
 *           - options    (required if no values supplied) - associative array
 *           - selected   (optional) - string default not set
 *           - output     (required if not options supplied) - array
 * Purpose:  Prints the list of <option> tags generated from
 *           the passed parameters
 * @link http://smarty.php.net/manual/en/language.function.html.options.php {html_image}
 *      (Smarty online manual)
 * @author Monte Ohrt <monte at ohrt dot com>
 * @param array
 * @param Smarty
 * @return string
 * @uses smarty_function_escape_special_chars()
 */
function smarty_function_pp_inputzones($params, &$smarty)
{
    require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
    
	$id = null;
    $name = null;
	$text = null;
    $value = null;
    $type = 'select';
    $target = 'zone';
	$ssd = 0;
	$country = 0;
    $output = null;
    $style = null;
    $extra = '';
    
    foreach($params as $_key => $_val) {
        switch($_key) {
			case 'id':
            case 'name':
			case 'text':
			case 'value':
			case 'type':
			case 'target':
			case 'ssd':
			case 'country':
                $$_key = (string)$_val;
                break;
				
            case 'style':
                $$_key = (string)$_val;
                break;
                
            default:
                if(!is_array($_val)) {
                    $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
                } else {
                    $smarty->trigger_error("pp_inputzones: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
                }
                break;
        }
    }
	
	//
	if( $country == 0 ) {
		$country = CONST_DEFAULT_COUNTRY_ID;
	}
	//
	$_html = '';
	if( $style == 'us_states' ) {
		$countries_id = 223;
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'zone.class.php');
		$zoneObj = new Zone();
		$zones = $zoneObj->getZones($countries_id);
		
		$_html .= '<select name="' . $name . '"' . (isset($id) ? ' id="' . $id . '"' : '') . $extra . '>';
		$_html .= '<option value="">Select State</option>';
		for($i=0, $n=sizeof($zones); $i<$n; $i++) {
			$_html .= '<option value="' . $zones[$i]['zones_code'] . '">' . $zones[$i]['zones_code'] . ' ' . $zones[$i]['zones_name'] . '</option>';
		}
		$_html .= '</select>';
	} else {
		/*$_html .= '<script language="javascript" type="text/javascript">PJS.addLoadScript(\'zone\');</script>';*/
		$_html .= '<input type="hidden" name="' . $name . '" id="' . $id . '" value="' . $value . '">';
		$_html .= '<input type="text" name="' . $name . '_text" id="' . $id . '_text" value="' . $text . '" class="inputText1" style="display: none;">';
		$_html .= '<div class="zones" id="' . $id . '_zones">';
		$_html .= '<div class="zone" id="' . $id . '_zone1"></div>';
		$_html .= '<div class="zone" id="' . $id . '_zone2"></div>';
		if( $type == 'parent' ) {
			//
		} elseif( $type == 'select' ) {
			$_html .= '<div class="zone" id="' . $id . '_zone3"></div>';
		}
		$_html .= '</div>';
		//PJS.addLoadEvent
		if( $country > 0 ) {
			$_html .= '<script language="javascript" type="text/javascript">zone.init(\'' . $id . '\', \'' . $ssd . '\');zone.load(\'' . $value . '\', \'' . $country . '\');</script>';
		} else {
			$_html .= '<script language="javascript" type="text/javascript">zone.init(\'' . $id . '\', \'' . $ssd . '\');zone.selected(\'' . $value . '\');</script>';
		}
	}
	
    return $_html;
}
//
?>